﻿#include "DeviceDemoValve.h"
#include "../PublicService/SettingData.h"
#include <thread>
#include <future>

using namespace DeviceLayer;

DYNAMIC_CREATE(DeviceDemoValve)

DeviceDemoValve::DeviceDemoValve()
{

}

bool DeviceDemoValve::On()
{
    onAsyncFuture = std::async(std::launch::async, [&]() -> void {
        std::this_thread::sleep_for(std::chrono::milliseconds(delay)); //Delay before switching the valve on.
        status = true;
    });
    return true;
}

bool DeviceDemoValve::Off()
{
    offAsyncFuture = std::async(std::launch::async, [&]() -> void {
        std::this_thread::sleep_for(std::chrono::milliseconds(delay)); //Delay before switching the valve off.
        status = false;
    });
    return true;
}

bool DeviceDemoValve::InitFromSettingData(const QString& path)
{
    //必须要调用父类的InitFromSettingData
    bool result = DeviceValveBase::InitFromSettingData(path);
    if (result)
    {
        delay = GET_SETTING_INT(path + "/Delay");
        return true;
    }
    else
        return false;
}

bool DeviceDemoValve::QueryData()
{
    PutDataToPool("valveStatus", status ? true : false);
    return true;
}
